library(CBPS)
set.seed(1111)
library(MatchIt)
library(dplyr)
library(ggplot2)
library(haven)
library(stargazer)
library(tableone)
library(Hmisc)
library(cem)
library(Matching)
library(car)
library(Zelig)
library(RColorBrewer)
library(arm)
setwd("~/Dropbox/AttorneyMainData")
data <- read_dta("Attorney.dta")
data$NPO <- car::recode(data$NPriorOral,"'20+'=20")
data$logNPO <- log(1+data$NPO)
data$hysLaw <- car::recode(data$SchoolLawyerArgue,"'Harvard'=1;'Yale'=1;'Stanford'=1;'Columbia'=1;'Chicago'=1;else=0")
data$demPres <- car::recode(data$SGOrals,"'McCree'=1;'Lee'=0;'(Acting) Fried'=0;'Fried'=0;'(Acting) Bryson'=0;'Starr'=0;'Days'=1;'(Acting) Dellinger'=1;'Waxman'=1;'(Acting) Underwood'=0; 'Olson'=0;'(Acting) Clement'=0;'Clement'=0;'Garre'=0;'(Acting) Kneedler'=1;'Kagan'=1;'(Acting) Katyal'=1;'Verrilli'=1;'(Acting) Gershengorn'=1;'(Acting) Francisco'=1;'(Acting) Wall'=1; 'Francisco'=1")
##Bryson was acting for both Bush 1 and Reagan
data$demPres[data$SGOrals=="(Acting) Bryson" & data$term=="1992"] <- 1
data$SGOralsCollapseF <- as.factor(data$SGOralsCollapse)
data$justiceNameF <- as.factor(data$justiceName)
data$ActingSGOrals <- as.numeric(factor(data$ActingSGOrals, ordered=F))-1
data$TopLawSchool <- as.numeric(factor(data$TopLawSchool, ordered=F))-1
data$DCFirm <- as.numeric(factor(data$DCFirm, ordered=F))-1
data$ClerkDummy <- as.numeric(factor(data$ClerkDummy, ordered=F))-1
data$OppPetOrResp <- as.numeric(factor(data$OppPetOrResp, ordered=F))-1
data$CriminalDummy <- as.numeric(factor(data$CriminalDummy, ordered=F))-1
data$CivLibDummy <- as.numeric(factor(data$CivLibDummy, ordered=F))-1
data$EconDummy <- as.numeric(factor(data$EconDummy, ordered=F))-1
data$Acela <- as.numeric(factor(data$Acela, ordered=F))-1
data$LawyerArgueOSG <- as.numeric(factor(data$LawyerArgueOSG, ordered=F))-1
data$GenderLawyerArgue <- as.numeric(factor(data$GenderLawyerArgue, ordered=F))-1
data$RaceLawyerArgue <- as.numeric(factor(data$RaceLawyerArgue, ordered=F))-1
data$IdeologyOppCounsel <- as.numeric(factor(data$IdeologyOppCounsel, ordered=F))-1
ctLevel <- subset(data, ct_level==1)
summary(data)
dim(data)


xvars<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres","justiceNameF")
data_nomiss <- data %>%  # MatchIt does not allow missing values
  dplyr::select(NPO, JVote, one_of(xvars)) %>%
  na.omit()
data_nomiss <- data.frame(data_nomiss)
xvars_case<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres")
data_case_nomiss <- ctLevel %>%  # MatchIt does not allow missing values
  dplyr::select(NPO, WhoWon, minWinning, unan, nearUnan, one_of(xvars_case)) %>%
  na.omit()
data_case_nomiss <- data.frame(data_case_nomiss)

fit_justice <- CBPS(NPO~SGOralsCollapseF+ActingSGOrals+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp+justiceNameF, data = data_nomiss,method="exact")
cbps_J <- glm(JVote~NPO+hysLaw+DCFirm+ClerkDummy+Rehnquist+Roberts+OppPetOrResp+IdeologyOppCounsel+demPres+justiceNameF, weights = fit_justice$weights,
            family = "quasibinomial", data=data_nomiss)
cbps_J_n <- glm(JVote ~ NPO, weights = fit_justice$weights,
              family = "quasibinomial", data=data_nomiss)

fit_case <- CBPS(NPO~SGOralsCollapseF+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp, data = data_case_nomiss, method="exact")
cbps_C <- glm(WhoWon~NPO+hysLaw+DCFirm+ClerkDummy+Rehnquist+Roberts+OppPetOrResp+IdeologyOppCounsel+demPres, weights = fit_case$weights,
            family = "quasibinomial", data=data_case_nomiss)
cbps_C_n <- glm(WhoWon ~ NPO, weights = fit_case$weights,
              family = "quasibinomial", data=data_case_nomiss)
unmatched_J <- glm(JVote ~ NPO+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp+justiceNameF,
              family = "quasibinomial", data=data_nomiss)
unmatched_C <- glm(WhoWon~NPO+hysLaw+DCFirm+ClerkDummy+Rehnquist+Roberts+OppPetOrResp+IdeologyOppCounsel+demPres,
              family = "quasibinomial", data=data_case_nomiss)
unmatched_J_n <- glm(JVote ~ NPO,
                   family = "quasibinomial", data=data_nomiss)
unmatched_C_n <- glm(WhoWon ~ NPO,
                   family = "quasibinomial", data=data_case_nomiss)
####LOG
xvars<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres","justiceNameF")
data_nomiss <- data %>%  # MatchIt does not allow missing values
  dplyr::select(logNPO, JVote, one_of(xvars)) %>%
  na.omit()
data_nomiss <- data.frame(data_nomiss)
xvars_case<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres")
data_case_nomiss <- ctLevel %>%  # MatchIt does not allow missing values
  dplyr::select(logNPO, WhoWon, minWinning, unan, nearUnan, one_of(xvars_case)) %>%
  na.omit()
data_case_nomiss <- data.frame(data_case_nomiss)

fit_justice <- CBPS(logNPO~SGOralsCollapseF+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp+justiceNameF, data = data_nomiss, method="exact")
cbps_J_log <- glm(JVote ~ logNPO+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp+justiceNameF, weights = fit_justice$weights,
            family = "quasibinomial", data=data_nomiss)
cbps_J_log_n <- glm(JVote ~ logNPO, weights = fit_justice$weights,
                  family = "quasibinomial", data=data_nomiss)

fit_case <- CBPS(logNPO~SGOralsCollapseF+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp, data = data_case_nomiss, method="exact")
cbps_C_log <- glm(WhoWon ~ logNPO+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp, weights = fit_case$weights,
            family = "quasibinomial", data=data_case_nomiss)
cbps_C_log_n <- glm(WhoWon ~ logNPO, weights = fit_case$weights,
                  family = "quasibinomial", data=data_case_nomiss)
unmatched_J_log <- glm(JVote ~ logNPO+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp+justiceNameF, 
                  family = "quasibinomial", data=data_nomiss)
unmatched_C_log <- glm(WhoWon ~ logNPO+hysLaw+DCFirm+ClerkDummy+IdeologyOppCounsel+demPres+Rehnquist+Roberts+OppPetOrResp, 
                  family = "quasibinomial", data=data_case_nomiss)
unmatched_J_log_n <- glm(JVote ~ logNPO, 
                       family = "quasibinomial", data=data_nomiss)
unmatched_C_log_n <- glm(WhoWon ~ logNPO, 
                       family = "quasibinomial", data=data_case_nomiss)

set.seed(1111)
aa <- sim(unmatched_J_n)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*1)
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*0 )
unmatched_J_n_j <- treated-untreated

aa <- sim(cbps_J_n)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*1)
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*0 )
cbps_J_n_j <- treated-untreated

aa <- sim(unmatched_J_log_n)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0.693147180559945 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0 )
unmatched_J_log_n_j <- treated-untreated

aa <- sim(cbps_J_log_n)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0.693147180559945 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0 )
cbps_J_log_n_j <- treated-untreated

dd <- data
aa <- sim(unmatched_J)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*1 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
unmatched_J_j <- treated-untreated

aa <- sim(cbps_J)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*1 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
cbps_J_j <- treated-untreated

aa <- sim(unmatched_J_log)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0.693147180559945 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
unmatched_J_log_j <- treated-untreated

aa <- sim(cbps_J_log)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0.693147180559945 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 + acoef[,"justiceNameFJPStevens"]*1 )
cbps_J_log_j <- treated-untreated

aa <- sim(unmatched_C)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*1 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
unmatched_C_j <- treated-untreated

aa <- sim(cbps_C)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*1 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"NPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
cbps_C_j <- treated-untreated

aa <- sim(unmatched_C_log)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0.693147180559945 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
unmatched_C_log_j <- treated-untreated

aa <- sim(cbps_C_log)
acoef <- coef(aa)
colnames(acoef)
treated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0.693147180559945 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0  )
untreated <- invlogit(acoef[,"(Intercept)"] + acoef[,"logNPO"]*0 + acoef[,"hysLaw"]*median(dd$hysLaw,na.omit=T) + acoef[,"DCFirm"]*median(dd$DCFirm,na.omit=T) + acoef[,"ClerkDummy"]*median(dd$ClerkDummy,na.omit=T) + acoef[,"Rehnquist"]*1 + acoef[,"Roberts"]*0 + acoef[,"OppPetOrResp"]*median(dd$OppPetOrResp,na.omit=T) + acoef[,"IdeologyOppCounsel"]*1  + acoef[,"demPres"]*0 )
cbps_C_log_j <- treated-untreated

###SI, Figure A3
pdf("Continuous.pdf",width=15,height=6.6,paper='special') 
par(mar=c(5.1,2, 4.1, 2.1),mfrow=c(1,2),oma=c(0,6,0,0))
plot(NA,xlim=c(-.05,.1),ylim=c(.5,2.5),ylab="",xlab="ATE", main="Naive Models",yaxt="n")
axis(2, at=c(2,1),labels=c("Count","Logged"), las=2)
points(y=2.1,x=mean(unmatched_J_n_j), pch=19, cex=1.25,col="gray70")
points(y=1.9,x=mean(cbps_J_n_j), pch=18, cex=1.5)
points(y=1.1,x=mean(unmatched_J_log_n_j), pch=19, cex=1.25,col="gray70")
points(y=.9,x=mean(cbps_J_log_n_j), pch=18, cex=1.5)
segments(y0=2.1,y1=2.1,x0=quantile(unmatched_J_n_j,.025),x1=quantile(unmatched_J_n_j,.975),lwd=2,col="gray70")
segments(y0=1.9,y1=1.9,x0=quantile(cbps_J_n_j,.025),x1=quantile(cbps_J_n_j,.975),lwd=2)
segments(y0=1.1,y1=1.1,x0=quantile(unmatched_J_log_n_j,.025),x1=quantile(unmatched_J_log_n_j,.975),lwd=2,col="gray70")
segments(y0=.9,y1=.9,x0=quantile(cbps_J_log_n_j,.025),x1=quantile(cbps_J_log_n_j,.975),lwd=2)
abline(v=0,lwd=3,col="gray60")

#Case-Level Figure
plot(NA,xlim=c(-.05,.1),ylim=c(.5,2.5),ylab="",xlab="ATE", main="Multivariate Models",yaxt="n")
points(y=2.1,x=mean(unmatched_J_j), pch=19, cex=1.25,col="gray70")
points(y=1.9,x=mean(cbps_J_j), pch=18, cex=1.5)
points(y=1.1,x=mean(unmatched_J_log_j), pch=19, cex=1.25,col="gray70")
points(y=.9,x=mean(cbps_J_log_j), pch=18, cex=1.5)
segments(y0=2.1,y1=2.1,x0=quantile(unmatched_J_j,.025),x1=quantile(unmatched_J_j,.975),lwd=2,col="gray70")
segments(y0=1.9,y1=1.9,x0=quantile(cbps_J_j,.025),x1=quantile(cbps_J_j,.975),lwd=2)
segments(y0=1.1,y1=1.1,x0=quantile(unmatched_J_log_j,.025),x1=quantile(unmatched_J_log_j,.975),lwd=2,col="gray70")
segments(y0=.9,y1=.9,x0=quantile(cbps_J_log_j,.025),x1=quantile(cbps_J_log_j,.975),lwd=2)
abline(v=0,lwd=3,col="gray60")
legend("bottomright",c("Full Dataset","Matched Dataset"),       col=c("gray70","black"),pch=c(19,18),pt.cex=c(1,1.3))
par(mar=c(5.1,4.1, 4.1, 2.1),oma=c(0,0,0,0))
par(mfrow=c(1,1))
dev.off()

###CEM
###NPO as treatment var
data$NPO1 <- car::recode(data$NPriorOral,"0=0;1=1;else=2")
ctLevel$NPO1 <- car::recode(ctLevel$NPriorOral,"0=0;1=1;else=2")
data$NPO2 <- car::recode(data$NPriorOral,"0=0;1=1;2=2;3=2;4=2;5=2;6=2;7=2;8=2;9=2;10=2;else=3")
table(data$NPO1)

##Three Category
xvars<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres","CriminalDummy","CivLibDummy","EconDummy","justiceNameF")
data_nomiss <- data %>%  # MatchIt does not allow missing values
  dplyr::select(NPO1, JVote, one_of(xvars)) %>%
  na.omit()
data_nomiss <- data.frame(data_nomiss)
xvars_case<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres","CriminalDummy","CivLibDummy","EconDummy")
data_case_nomiss <- ctLevel %>%  # MatchIt does not allow missing values
  dplyr::select(NPO1, WhoWon, one_of(xvars_case)) %>%
  na.omit()
data_case_nomiss <- data.frame(data_case_nomiss)
cem_J <- cem(treatment="NPO1", data=data_nomiss, drop=c("JVote"))
a <- att(cem_J, JVote ~ NPO1, data=data_nomiss,model="logit")
a

##Four Category
xvars<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres","CriminalDummy","CivLibDummy","EconDummy","justiceNameF")
data_nomiss <- data %>%  # MatchIt does not allow missing values
  dplyr::select(NPO2, JVote, one_of(xvars)) %>%
  na.omit()
data_nomiss <- data.frame(data_nomiss)
xvars_case<-c("SGOralsCollapseF","ActingSGOrals","hysLaw","DCFirm","ClerkDummy","IdeologyOppCounsel","Rehnquist","Roberts","OppPetOrResp","demPres","CriminalDummy","CivLibDummy","EconDummy")
data_case_nomiss <- ctLevel %>%  # MatchIt does not allow missing values
  dplyr::select(NPO1, WhoWon, one_of(xvars_case)) %>%
  na.omit()
data_case_nomiss <- data.frame(data_case_nomiss)
cem_J <- cem(treatment="NPO2", data=data_nomiss, drop=c("JVote"))
a <- att(cem_J, JVote ~ NPO2, data=data_nomiss,model="logit")
a
